package com.park.tool;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

/**
 * Created by park-tool on 2020/8/28
 */
public class DesUtil {

    public static String encrypt(String text) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec("6D4267DF81D83449D851617E".getBytes("utf-8"));
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);

            Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec("78549912".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            byte[] res = cipher.doFinal(text.getBytes("utf-8"));
            //encodeBase64会对字符串3位一组自动补全，因而最后可能会出现 == 或者 =
            return new String(Base64.encodeBase64(res), "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
